import oss2
import uuid
import os

def upload_file(imgpath):
    # 配置访问密钥和Endpoint
    access_key_id = '0LHcQs2r5xzS5jGV'
    access_key_secret = 'ADvFq4wjO821Q2semQdylVfu9RMkBa'
    endpoint = 'https://oss-cn-beijing.aliyuncs.com'

    # 创建 OSS 客户端实例
    auth = oss2.Auth(access_key_id, access_key_secret)
    bucket = oss2.Bucket(auth, endpoint, 'langfang-img')

    # 生成唯一的文件名
    unique_filename = str(uuid.uuid4())

    # 拼接图片名
    oss_file_path = unique_filename + '.jpg'
    try:
        bucket.put_object_from_file(oss_file_path, imgpath)
        # print('上传成功')
        return 'https://langfang-img.oss-cn-beijing.aliyuncs.com/' + oss_file_path
    except oss2.exceptions.OssError as e:
        # print('上传失败')
        return ''
    
if __name__ == '__main__':

    # upload_file('./test/image.png')
    pass